﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using GameService;

namespace Website
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Master.Initialize();
            if (Master.IsLoggedIn)
            {
                Response.Redirect("gamelist.aspx");
            }
            else if (Master.IsConnected() && Master.FbUser != null)
            {
                string name = Master.FbUser.name;
                bool success = false;

                AuthenticationResult authentication_Res = Master.GameManager.AuthenticateFacebook(name);
                if (authentication_Res == AuthenticationResult.IncorrectUserName ||
                    authentication_Res == AuthenticationResult.IncorrectPassword)
                    lblMessage.Text = "Incorrect username or password";
                else if (authentication_Res == AuthenticationResult.UserAlreadyLoggedIn)
                    lblMessage.Text = "User is already logged in";
                else if (authentication_Res == AuthenticationResult.UnknownError)
                    lblMessage.Text = "Login failed due to unknown reason";
                else
                    success = true;


                if (success)
                {
                    Master.SetUser(name);
                    Master.IsFacebook = true;
                    Response.Redirect("gamelist.aspx");
                }
                else
                {
                    lblMessage.Visible = true;
                }
            }
            else
            {
                Master.IsFacebook = false;
            }
        }


        protected void btnGuest_Click(object sender, EventArgs e)
        {
            string name;
            if (Master.FbUser != null)
            {
                name = Master.GameManager.Guest_Connection(Master.FbUser.name);
            }
            else
            {
                name = Master.GameManager.Guest_Connection();
            }
            Master.SetUser(name);
            Master.IsGuest = true;
            Response.Redirect("gamelist.aspx");
        }
    }
}
